package java8.stream.demo09practice;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * @author ChangLiang
 * @date 2020/4/23
 */
public class AppTest extends AbstractTraderBeforeTest {

    @Test
    @DisplayName("1. Find all transactions in the year 2011 and sort them by value (small to high)")
    public void test(){
        System.out.println(transactions.stream().filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue)).collect(toList()));
    }
    
    @Test
    @DisplayName("1. Find all transactions in the year 2011 and sort them by value (high to small)")
    public void test2(){
        System.out.println(transactions.stream().filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue).reversed()).collect(toList()));
    }
    
    @Test
    @DisplayName("2. What are all the unique cities where the traders work?")
    public void test3(){
        transactions.stream().map(t->t.getTrader().getCity())
                .distinct().collect(toList())
                .forEach(System.out::println);
    }
    
    @Test
    @DisplayName("3. Find all traders from Cambridge and sort them by name")
    public void test4(){
        transactions.stream()
                .map(Transaction::getTrader)
                .distinct()
                .filter(t->t.getCity().equals("Cambridge"))
                .sorted(Comparator.comparing(Trader::getName))
                .collect(toList())
                .forEach(System.out::println);
    }

    /**
     * output: Alan; Brian; Mario; Raoul;
     */
    @Test
    @DisplayName("4. Return a string of all traders’ names sorted alphabetically")
    public void test5(){
        String result = transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (n1, n2) -> n1 + n2 + "; ");
        System.out.println(result);
    }

    @Test
    @DisplayName("5. Are any traders based in Milan?")
    public void test6() {
        boolean result = transactions.stream()
                .map(Transaction::getTrader)
                .anyMatch(t -> t.getCity() == "Milan");
        System.out.println(result);
    }
    
    @Test
    @DisplayName("6. Print all transactions’ values from the traders living in Cambridge.")
    public void test7(){
        transactions.stream()
                .filter(t->t.getTrader().getCity().equals("Cambridge"))
                .collect(toList())
                .forEach(System.out::println);
    }
    
    @Test
    @DisplayName("7. What’s the highest value of all the transactions?")
    public void test8(){
        Optional<Integer> optional = transactions.stream()
                .map(t -> t.getValue())
                .reduce(Integer::max);
        optional.ifPresent(System.out::println);
    }
    
    @Test
    @DisplayName("8. Find the transaction with the smallest value.")
    public void test9(){
        Optional<Integer> optional = transactions.stream()
                .map(t -> t.getValue())
                .reduce(Integer::min);
        optional.ifPresent(System.out::println);
    }
    
}
